package cn.wolfcode.luowowo.serach.service.impl;

import cn.wolfcode.luowowo.search.repository.IDestinationTemplateRepository;
import cn.wolfcode.luowowo.search.service.IDestinationSearchService;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class DestinationSearchServiceImpl implements IDestinationSearchService {

    @Autowired
    private IDestinationTemplateRepository dao;

    @Autowired
    private ElasticsearchTemplate template;

    @Autowired
    private TransportClient client;

    public void createIndex(){
        template.createIndex(DestinationTemplate.class);
        template.putMapping(DestinationTemplate.class);
    }

    public void save(DestinationTemplate tt) {
        dao.save(tt);
    }

    public DestinationTemplate queryDestByName(String name) {
        //在这里面在查询页面所需要的值
        Iterable<DestinationTemplate> iterable = dao.search(QueryBuilders.termQuery("name", name));
        List<DestinationTemplate> list = new ArrayList<>();
        iterable.forEach(s -> {
            list.add(s);
        });
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
